import java.util.StringTokenizer;

import twitter4j.TwitterException;
import twitter4j.examples.twitterLib;

public class TwitterManager extends twitterLib{

	/****************************************
	 * Attributes
	 ****************************************/
	private String[] happyKeywords = {"happy","joyful"};
	private String[] sadKeywords = {"sad","unhappy","cry"};
	private String[] dangerKeywords = {"earthquake","help","sos"};
	
	// Flags
	private final int NORMAL = 0;
	private final int HAPPY = 1;
	private final int SAD = 2;
	private final int DANGER = 3;
	
	
	/****************************************
	 * Constructor
	 ****************************************/
	public TwitterManager() {
		super();
	}
	
	
	// Methods
	/****************************************
	 * Connect to Twitter
	 ****************************************/
	public void connectToTwitter() {
		this.setkey("YlvdQJxge4AA8W7rk9NIg", "eVLtoAhGPI52dyh5toDyrJB6ye1H7iHrb5YxUXANds");
		this.setToken("275559715-TfGQIZKDrCDtaYmkOOYflBr4W28RkhP869NsTaZM", "EwgamkxtyCGH4MuvMjFp4eHyLsg0XD1NRXYTY1GZc");
		this.accessTwitter();
	}
	
	
	/****************************************
	 * Get the latest tweet
	 ****************************************/
	public String getLatestTweet() {
		try {
			return getLastestStatus();	
			
		} catch (TwitterException e) {
			return "TwitterException: Cannot get latest tweet";
		}
	}
	
	
	/****************************************
	 * Get the latest tweet's userId
	 ****************************************/
	public String getLatestTweetId() {
		return null; /** To be modified **/
		
	}
	
	
	/****************************************
	 * Tweet a message
	 ****************************************/
	public void tweet(String tweet) {
		try {
			super.tweet(tweet);
			
		} catch (TwitterException e) {
			System.out.println("TwitterException: Cannot tweet");
		}
	}
	
	
	/****************************************
	 * Check whether a tweet is sad
	 ****************************************/
	public boolean tweetIsSad(String tweet) {	
		return tweetAnalyse(tweet) == SAD;	
	}
	
	
	/****************************************
	 * Check whether a tweet is happy
	 ****************************************/
	public boolean tweetIsHappy(String tweet) {
		return tweetAnalyse(tweet) == HAPPY;
	}
	
	
	/****************************************
	 * Check whether a tweet is danger
	 ****************************************/
	public boolean tweetIsDanger(String tweet) {
		return tweetAnalyse(tweet) == DANGER;
	}
	
	
	// Private Methods
	/****************************************
	 * Tweet analyse
	 ****************************************/
	private int tweetAnalyse(String tweet){
		StringTokenizer tokenStream = new StringTokenizer(tweet);
		
		 while (tokenStream.hasMoreTokens()) {
			 
			String curToken = tokenStream.nextToken().toString().toLowerCase();
			
			// Check whether it is happy
			for (int j=0; j < happyKeywords.length; j++) {
				if (curToken.equals(happyKeywords[j])) {
					return HAPPY;
				}
			}
			
			// Check whether it is sad
			for (int j=0; j < sadKeywords.length; j++) {
				if (curToken.equals(sadKeywords[j])) {
					return SAD;
				}
			}
			
			// Check whether it is danger
			for (int j=0; j < dangerKeywords.length; j++) {
				if (curToken.equals(dangerKeywords[j])) {
					return DANGER;
				}
			}
		}
		return NORMAL;
	}
}
